/**
 * 1.初始化 Routers
 * 2.路由hash的存储与执行（hashchange）
 *
 * */

class Routers {
  constructor() {
    this.routes = {};
    this.currentUrl = "";
    this.refresh = this.refresh.bind(this);
    window.addEventListener("load", this.refresh, false);
    window.addEventListener("hashchange", this.refresh, false);
  }
  // 将path路径与对应的callback函数储存
  route(path, callback) {
    this.routes[path] = callback || function () {};
  }

  refresh() {
    // 获取当前URL中的hash路径
    this.currentUrl = location.hash.slice(1) || "/";
    // 执行当前hash路径的callback函数
    this.routes[this.currentUrl]();
  }
}

window.Router = new Routers();
var content = document.querySelector("body");
// change Page anything
function changeBgColor(color) {
  content.style.backgroundColor = color;
}
Router.route("/", function () {
  changeBgColor("yellow");
});
Router.route("/blue", function () {
  changeBgColor("blue");
});
Router.route("/green", function () {
  changeBgColor("green");
});
